<?php
// 课程模型
class CourseModel extends CommonModel {
	
	protected $_validate = array(
	// 			array('XS_XH','','学号已经存在！',0,'unique',1), // 在新增的时候验证字段是否唯一
	//         array('XS_XM','require','姓名不能为空'),
	//         array('XS_RXRQ','require','入学日期不能为空'),
	//         array('XS_XX','require','就读学校不能为空'),
	//         array('XS_NJ','require','就读年级不能为空'),
	//         array('XS_BJ','require','就读班级不能为空'),
	//         array('XS_JTZZ','require','家庭住址不能为空'),
	//         array('XS_JTDH','require','家庭电话不能为空'),
	//         array('XS_JZXM','require','家长姓名不能为空'),
	//         array('XS_JZDH','require','家长电话不能为空'),
	//         array('XS_JZMAIL','require','家长Email不能为空'),
	);
	
	protected $_auto =	array(
// 			array('XS_XH','getStuXH',1,'callback'),
// 			array('XS_PY','getStuPY',3,'callback'),
			array('status','1',self::MODEL_INSERT),  // 新增的时候把status字段设置为1 活跃
			//array('create_time','time',self::MODEL_INSERT,'function'),
	);
	
	public function getModeDisplayList($xq,$kcmc,$km,$offset,$rows,$sort,$order){
		
		$result = array();
		$kc_xq ='';
		$KC_BJMC='';		
		if(!empty($xq)){
			$kc_xq = " and kc_xq in(". $xq.")";
		}
		if(!empty($km)){
			$KC_BJMC = " and  kc_kmbh='".$km ."'";
		}
		$kcmc = mysql_real_escape_string($kcmc);
		$where = "KC_BJMC like '%". $kcmc."%' and status=1 " . $kc_xq . $KC_BJMC;
		
		$sql = "select t.*,t1.ITEM_VALUE as XQ,t2.ITEM_VALUE AS NJ,t3.ITEM_VALUE AS KM,t4.nickname AS BZR,t5.nickname AS TEACHER
					from  (select * from ".$this->tablePrefix."course  where " . $where . " ) as t
					join ".$this->tablePrefix."dictionary_list t1 on t.kc_xq = t1.ITEM_KEY 
					join ".$this->tablePrefix."dictionary_list t2 on t.kc_nj = t2.ITEM_KEY 
					join ".$this->tablePrefix."dictionary_list t3 on t.kc_kmbh = t3.ITEM_KEY
					join ".$this->tablePrefix."user t4 on t.kc_bzr=t4.id
					join ".$this->tablePrefix."user t5 on t.kc_lsbh=t5.id order by t." . $sort . " " . $order;
		
		$arrNum = $this->db->query("select count(*) as num from ".$this->tablePrefix."course where " . $where);
		$row = $arrNum[0]['num'];
		
		$result["total"] = $row;
		$rs = $this->db->query($sql . " limit $offset,$rows");
// 		$items = array();
// 		while($row = mysql_fetch_object($rs)){
// 			array_push($items, $row);
// 		}
		$result["rows"] = $rs;
		
		return $result;
	}
}